<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>医院信息</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<jsp:include page="${pageContext.request.contextPath }/resource_path.jsp" flush="true"/>
</head>
<body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a>
          <cite>医院信息</cite>
        </a>
      </span>
    </div>
    <div class="x-body">
      <div style="margin-bottom: 18px">
		<div class="layui-inline">
			<table>
				<tr>
					<td>
						<input class="layui-input" name="keyword01" id="keyword01" placeholder="请输入医院名称" autocomplete="off" style="display: inline; width: 180px; margin-left: 10px">
					</td>
					<td>
						<input class="layui-input" name="keyword02" id="keyword02" placeholder="请输入级别" autocomplete="off" style="display: inline; width: 180px; margin-left: 10px">
					</td>
				</tr>
			</table>
		</div>
		<button class="layui-btn layui-btn-normal" data-type="reload"><i class="layui-icon">&#xe615;</i>检索</button>
	  </div>
      <xblock>
			<button id="add" class="layui-btn layui-btn-blue"><i class="layui-icon"></i>新增</button>
			<button id="dels" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-delete"></i>批量禁用</button>
      </xblock> 
	  <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
	</div>
	
	<script type="text/html" id="rowToolbar">
		<input  style="margin-left:150px;" type="checkbox" name="lock" value="{{d.mid}}" title="启用" lay-filter="lockDemo" {{ d.remark9 == 1? 'checked' : '' }}>
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">查看</a>
	</script>
	
	<!-- 新增框 -->
	<div id="addDivID" hidden="hidden">
		<form class="layui-form" id="addFormID">
		
			<br>
			<div class="layui-form-item">
			    <div class="layui-inline">
			      <label class="layui-form-label">医院名称</label>
			      <div class="layui-input-inline">
			        <input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
			      </div>
			    </div>
			    <div class="layui-inline">
			      <label class="layui-form-label">级别</label>
			      <div class="layui-input-inline">
			        <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
			      </div>
			    </div>
	  		</div>
	  		
			<div class="layui-form-item">
			    <div class="layui-form-item layui-form-text">
				   <label class="layui-form-label">地&emsp;&emsp;址</label>
				   <div class="layui-input-block" style="width: 515px;">
				     <textarea placeholder="请输入内容" class="layui-textarea"></textarea>
				   </div>
				</div>
			</div>
			
			<div class="layui-form-item">
			    <div class="layui-inline">
			      <label class="layui-form-label">邮编</label>
			      <div class="layui-input-inline">
			        <input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
			      </div>
			    </div>
			    <div class="layui-inline">
			      <label class="layui-form-label">电话</label>
			      <div class="layui-input-inline">
			        <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
			      </div>
			    </div>
	  		</div>
	  		
			<div class="layui-form-item">
			    <div class="layui-inline">
			      <label class="layui-form-label">传真</label>
			      <div class="layui-input-inline">
			        <input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
			      </div>
			    </div>
			    <div class="layui-inline">
			      <label class="layui-form-label">网站</label>
			      <div class="layui-input-inline">
			        <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
			      </div>
			    </div>
	  		</div>
	  		
	  		<div class="layui-form-item">
			  <label class="layui-form-label">社会保障&emsp;信息用代码</label>
			  <div class="layui-input-block" style="width: 515px;">
			    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
			  </div>
			</div>
	  		<div class="layui-form-item">
			  <label class="layui-form-label">行政区划&emsp;编码&emsp;&emsp;</label>
			  <div class="layui-input-block" style="width: 515px;">
			    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
			  </div>
			</div>
	  		<div class="layui-form-item">
			  <label class="layui-form-label">前置机地址</label>
			  <div class="layui-input-block" style="width: 515px;">
			    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
			  </div>
			</div>
			<div class="layui-form-item">
			    <div class="layui-form-item layui-form-text">
				   <label class="layui-form-label">医院简介</label>
				   <div class="layui-input-block" style="width: 515px;">
				     <textarea placeholder="请输入内容" class="layui-textarea"></textarea>
				   </div>
				</div>
			</div>
			<div class="layui-form-item">
			  <label class="layui-form-label">院区标记</label>
			  <div class="layui-input-block" style="width: 515px;">
			    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
			  </div>
			</div>
			 <div class="layui-form-item" pane="">
			    <label class="layui-form-label">开通类型</label>
			    <div class="layui-input-block">
			      <input type="checkbox" name="like1[write]" lay-skin="primary" title="药品" checked="">
			      <input type="checkbox" name="like1[read]" lay-skin="primary" title="其他">
			      <input type="checkbox" name="like1[read]" lay-skin="primary" title="设备">
			      <input type="checkbox" name="like1[game]" lay-skin="primary" title="物资">
			      <input type="checkbox" name="like1[game]" lay-skin="primary" title="卫生材料">
			      <input type="checkbox" name="like1[game]" lay-skin="primary" title="医用耗材">
			    </div>
			  </div>
			 <div class="layui-form-item">
			    <div class="layui-input-block">
			      <button class="layui-btn layui-btn-blue" lay-submit lay-filter="addForm" style="margin-left:140px;">提交</button>
			      <button class="layui-btn layui-btn-primary">取消</button>
			    </div>
			  </div>
		</form>
    </div>

	<script type="text/javascript">
        var table;
		layui.use(['table','layer','upload','form','laydate'], function(){
			table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var laydate = layui.laydate;
			var $ = layui.jquery, active = {
				reload:function () {
					var keyword01 = $.trim($("#keyword01").val());
	                var keyword02 = $.trim($("#keyword02").val());
					table.reload('contenttable',{
						method:'get',
						where:{
							number:keyword01
	                        ,name:keyword02
						}
						,page: {
		                    curr: 1//重新从第 1 页开始
		                }
					});
				}
			}
			$('.layui-btn').on('click', function(){
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			table.render({
				elem: '#tableList'
				,url:'/json/list1.json'
				,toolbar: '#toolbar'
				,title: 'machineList'
				,id :'contenttable'
				,limits:[10,20,30]
				,request: {   //如果无需自定义请求参数，可不加该参数
	                pageName: 'page' //页码的参数名称，默认：page
	                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
	            }
	            ,response: {   //如果无需自定义数据响应名称，可不加该参数
	                countName: 'total' //规定数据总数的字段名称，默认：count
	                ,dataName: 'rows' //规定数据列表的字段名称，默认：data
	            }
				,cols: [
		                [{
		                    type: 'checkbox',
		                    fixed: 'left'
		                }, {
		                	field:'id',
		                	title:'序号', 
		                	sort: true,
		                	width: 60,
		                	type:'numbers',
		                	align:'center'
		                }, {
		                    field: 'mid',
		                    title: 'id',
		                    hide:true 
		                } , {
		                    field: 'remark1',
		                    title: '医院名称',
		                    width: 100,
		                    align:'center'
		                } , {
		                    field: 'number',
		                    title: '级别',
		                    width: 120,
		                    align:'center',
		                }, {
		                    field: 'name',
		                    title: '地址',
		                    align:'center'
		                }, {
		                    field: 'cname',
		                    title: '邮编',
		                    align:'center'
		                }, {
		                    field: 'unit',
		                    title: '电话',
		                    align:'center'
		                }, {
		                    field: 'remark2',
		                    title: '传真',
		                    align:'center'
		                }, {
		                    field: 'remark3',
		                    title: '网站',
		                    align:'center'
		                }, {
		                    field: 'remark3',
		                    title: '社会保障信息用代码',
		                    align:'center'
		                }, {
		                    field: 'remark5',
		                    title: '行政区划编码',
		                    align:'center' 
		                 }, {
		                    field: 'remark6',
		                    title: '前置机地址',
		                    align:'center' 
		                }, {
		                    field: 'remark6',
		                    title: '医院简介',
		                    align:'center' 
		                }, {
		                    field: 'remark6',
		                    title: '院区标记',
		                    align:'center' 
		                }, {
		                    field: 'remark6',
		                    title: '开通类型',
		                    align:'center' 
		                }, {
		                    fixed: 'right',
		                    title:'操作',
		                    toolbar: '#rowToolbar',
		                    align:'center',
		                    unresize: true,
		                    width: 250 
		                }
		                ]
		               ]
				,page: true
				,done:function(){
			    	$('th').css({
		                'background-color': '#009688', 'color': '#fff','font-weight':'bold',
		            })
			    }
			});
			
			

            //监听锁定操作
            form.on('checkbox(lockDemo)', function(obj){
            	
            	var mid = this.value;
            	var remark9 ;
            	if(obj.elem.checked==true) {
            		remark9 = 1;
            		$.ajax({
    					type:'post',
    					url:'${pageContext.request.contextPath }/materiel/use.do',
    					data:{ "mid" : mid,"remark9":remark9},
    					success:function(data){
    						if(data > 0){
    							toastr.success("启用成功！");
    						}else {
    							toastr.success("启用失败，请联系管理员");
    						}
    					}
    				});
            	}else {
            		remark9 = 0;
            		$.ajax({
    					type:'post',
    					url:'${pageContext.request.contextPath }/materiel/use.do',
    					data:{ "mid" : mid,"remark9":remark9},
    					success:function(data){
    						if(data > 0){
    							toastr.success("已关闭！");
    						}else {
    							toastr.success("关闭失败，请联系管理员");
    						}
    					}
    				});
            	}
            	
            });
            
			//监听行工具事件
			table.on('tool(tableList)', function(obj){
				var data = obj.data;
				//单个禁用
				if(obj.event === 'del'){
					
				  layer.confirm('确定禁用吗？', function(index){
					var id = obj.data.mid;
					$.ajax({
						type:'post',
						url:'${pageContext.request.contextPath }/materiel/delete.do',
						data:{ "ids" : id },
						success:function(data){
							if(data > 0){
								toastr.success("禁用成功！");
								setTimeout(function(){
									location.reload();
								},1000);
							}else{
								toastr.warning("禁用失败！");
								setTimeout(function(){
									location.reload();
								},1000);
							}
						}
					});
					layer.close(index);
				  });
				  
				} else if(obj.event === 'edit'){
					
					layer.open({
					  type: 1 							//Page层类型
					  ,area: ['700px', '830px'] //宽  高
					  ,title: '编辑'
					  ,shade: 0.6 						//遮罩透明度
					  ,maxmin: true 					//允许全屏最小化
					  ,anim: 1 							//0-6的动画形式，-1不开启
					  ,content: $('#addDivID')
                      ,end: function () {
                          var formDiv = document.getElementById('addDivID');
                          formDiv.style.display = '';
                      }
                      ,success: function(){
                    	 /*  $("#mid").val(data.mid)
                    	  $("#remark1").val(data.remark1)
                    	  $("#number").val(data.number)
                    	  $("#name").val(data.name)
                    	  $("#cname").val(data.cname)
                    	  $("#unit").val(data.unit)
                    	  $("#remark2").val(data.remark2)
                    	  $("#remark3").val(data.remark3)
                    	  //if(data.remark4 != ""){
	                    	  $("#remark4").val(data.remark4)
                    	  //}else{
                    		//  $("#remark4 option:first").prop("selected", 'selected'); 
                    	  //}
                    	  $("#remark5").val(data.remark5)
                    	  $("#remark6").val(data.remark6)
                    	  $("#remark7").val(data.remark7)
                    	  $("#remark8").val(data.remark8) */
                          form.render();
                      }
					});
				}
				
			});

			
			/**
			 * 通用表单提交(AJAX方式)(新增)
			 */
			form.on('submit(addForm)', function (data) {
				var index = layer.load(1, {
	        	    shade: [0.1,'#000'] //0.1透明度的背景
	        	});
				
				var formData = $("#addFormID").serialize();
				var url = '${pageContext.request.contextPath}/materiel/insert.do';
				var title = "新增";
				var mid = $.trim($("#mid").val());
				if(mid != null && mid != "" && mid != 'undefined'){
					url = '${pageContext.request.contextPath}/materiel/update.do';
					title = "修改";
				} 
				$.ajax({
					url : url,
					data: formData,
					cache : false,
					type : "post",
					success: function (data) {
	                    if (data >= 1) {
	                        toastr.success(title+'成功');
	                        setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	                            window.location.reload();
	                            $("#reset").click();
	                        },1000);
	                    } else {
	                        toastr.error(title+'失败，该品编、中文名称、工厂不能同时重复');
	                    }
	                },
				});
				layer.close(index);
				return false;
			});
			

			/**
	         * 表单校验
	         */
	        form.verify({
	            //value：表单的值、item：表单的DOM对象
	            required: function(value, item){
	            	value = $.trim(value)
	            	if(value == '' || value == null){
	                    return '不能为空';
	                }
	            },
	            //value：表单的值、item：表单的DOM对象
	            number: function(value, item){
	            	value = $.trim(value)
	            	var re = /^[0-9]+$/ ;
	            	if(value != '' && value != null && !re.test(value)){
	                    return '请输入正整数';
	                }
	            },
	            //value：表单的值、item：表单的DOM对象
	            only: function(value, item){
	            	value = $.trim(value)
	                if(value == '' || value == null){
	                    return '不能为空';
	                }else{
	                    var flg = false;
	                    var number = $.trim($("#number").val());
	                    var cname = $.trim($("#cname").val());
	                    var remark7 = $.trim($("#remark7").val());
	                    var remark8 = $.trim($("#remark8").val());
	                	if(number == "" || cname == "" || number == null || cname == null || remark7=="" || remark7 == null || remark8=="" || remark8 == null){
	                		flg  = true;
	                	}else{
		                    $.ajax({
		                        type:'post',
		                        data:'number='+number+"&"+"cname="+cname+"&"+"remark7="+remark7+"&"+"remark8="+remark8,
		                        url:'${pageContext.request.contextPath}/materiel/queryAllByMution.do',
		                        dataType: 'JSON',
		                        async: false,
		                        success:function (data) {
		                            if(data.length == 0){
		                                flg  = true;
		                            }else if(data.length == 1){
		                                var ma = $.trim($("#mid").val());
		                                if (ma == data[0].mid) {
		                                    flg  = true;
		                                }else{
		                                    flg = false;
		                                }
		                            }else{
		                                flg = false;
		                            }
	
		                        }
		                    });
	                	}
	                    if(!flg){
	                        //return '品编、中文名称和工厂不能同时重复';
	                        return '品编、中文名称、工厂和厂别不能同时重复';
	                    }
	                }
	            }
	        });
			
			//增加
            $("#add").click(function(){
                layer.open({
                    type: 1 					//Page层类型
                    ,area: ['700px', '830px'] //宽  高
                    ,title: '新增'
                    ,shade: 0.6 				//遮罩透明度
                    ,maxmin: true 				//允许全屏最小化
                    ,anim: 1 					//0-6的动画形式，-1不开启
                    ,content: $('#addDivID')
                    ,end: function () {
                        var formDiv = document.getElementById('addDivID');
                        formDiv.style.display = '';
                    }
                    ,success: function(){
                    	/* $("#mid").val("")
                  	    $("#remark1").val("")
                  	    $("#number").val("")
                  	    $("#name").val("")
                  	    $("#cname").val("")
                  	    $("#unit").val("")
                  	    $("#remark2").val("")
                   	    $("#remark3").val("")
                   		$("#remark4 option:first").prop("selected", 'selected'); 
                   	    $("#remark5").val("")
                   		$("#remark6 option:first").prop("selected", 'selected'); 
                   		$("#remark7 option:first").prop("selected", 'selected'); 
                   		$("#remark8 option:first").prop("selected", 'selected');  */
                        form.render();
                    }
                });
			});

          //批量禁用
			$("#dels").click(function(){
				var checkStatus = table.checkStatus('contenttable')
	      		var data = checkStatus.data;
	            if(data.length==0){
	                toastr.warning("请至少选择一条记录！");
	            }else{
	                var ids = "";
	                for(var i=0;i<data.length;i++){
	                    if(i == 0 || i == "0"){
	                        ids += data[i].mid
	                    }else{
	                        ids += "," + data[i].mid;
	                    }
	                }
	                layer.confirm('确定禁用吗？', function(index){
	                    $.ajax({
	                        type:'post',
	                        url:'${pageContext.request.contextPath }/materiel/delete.do',
	                        data:{"ids":ids},
	                        success:function(data){
	                            if(data>0){
	                                toastr.success("禁用成功！");
	                                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	                                    //关闭模态框
	                                    // 父页面刷新
	                                    window.location.reload();
	                                },2000);
	                            }else{
	                                toastr.warning("禁用失败！");
	                            }
	                        }

	                    })
	                    layer.closeAll();
	                });
	            }
			});
		});
		
		toastrStyle()
	</script>
</body>
</html>